#!/bin/bash

# 这个脚本用于将临时保存的内容移动到用户指定的目录

if [ -z "$RFM_TEMP_SAVE_PATH" ]; then
    echo "错误: 未找到临时保存路径" >&2
    exit 1
fi

if [ ! -d "$RFM_TEMP_SAVE_PATH" ]; then
    echo "错误: 临时目录不存在: $RFM_TEMP_SAVE_PATH" >&2
    exit 1
fi

# 获取当前目录信息
if [ ! -z "$rfm_curPath" ]; then
    cd "$rfm_curPath"
fi

echo "当前内容已临时保存在: $RFM_TEMP_SAVE_PATH"
echo
echo "直接回车将对话记录移动到当前目录"
echo "或输入目标路径（绝对路径或相对于当前路径($(pwd))）:"

source rfmReadlineWithSpecificHistoryFile.sh "dirname"  ~/.rfm_history_directory

if [ ! -z "$dirname" ]; then
    dirname=${dirname/#~/${HOME}}
    cd "$dirname"
fi

# 移动临时目录到目标位置
temp_basename=$(basename "$RFM_TEMP_SAVE_PATH")
if mv "$RFM_TEMP_SAVE_PATH" .; then
 RFM_FINAL_SAVE_PATH="$(pwd)/$temp_basename"
 export RFM_FINAL_SAVE_PATH
 
 echo
 echo "对话记录已保存至: $RFM_FINAL_SAVE_PATH" >&2
 echo "主文件: $RFM_FINAL_SAVE_PATH/rfmAI.md" >&2
 
 # 列出所有附件
 if ls "$RFM_FINAL_SAVE_PATH"/*.png "$RFM_FINAL_SAVE_PATH"/*.jpg "$RFM_FINAL_SAVE_PATH"/*.jpeg >/dev/null 2>&1; then
 echo "附件文件:"
 ls "$RFM_FINAL_SAVE_PATH"/*.png "$RFM_FINAL_SAVE_PATH"/*.jpg "$RFM_FINAL_SAVE_PATH"/*.jpeg 2>/dev/null | while read file; do
 echo " $(basename "$file")"
 done
 fi
else
 echo "错误: 移动文件失败" >&2
 exit 1
fi
